class Chess {
	board = new Board();
	view = "red";
	pieces = [];
	selects = [];
	mays = [];
	
	history=[];
	fen="";

	show = function() {
		chess.board.init(chess.view);
		chess.board.pieces(chess.pieces, chess.view);
		chess.board.selects(chess.selects, chess.view);
		chess.board.mays(chess.mays, chess.view);
	};

	flip = function() {
		if (this.view == "black") {
			this.view = "red";
		} else {
			this.view = "black";
		}
		this.show();
	};
	
	submit=function(){
		let fen = document.getElementsByName("fen")[0].value;
		this.fen=fen;
		let arr = fenToArray(fen);
		let pieces = arrToPieces(arr);
		this.pieces = pieces;
		this.selects=[];
		this.mays=[];
		this.history=[];
		
		this.show();
	};
	
	back=function(){
		let arr = fenToArray(this.fen);
		let pieces = arrToPieces(arr);
		this.pieces = pieces;
		this.selects=[];
		this.mays=[];
		if(this.history.length>0){
			this.history.pop();
			for(let i=0;i<this.history.length;i++){
				this.go(this.history[i][0],this.history[i][1],this.history[i][2],this.history[i][3]);
			}
		}else{
			alert("未开始，无法后退");
		}
		this.show();
	};
	
	go=function(x1,y1,x2,y2){
		let arr = piecesToArray(chess.pieces);
		arr[x2][y2]=arr[x1][y1];
		arr[x1][y1]=0;
		this.pieces=arrToPieces(arr);
		this.selects=[{x:x1,y:y1},{x:x2,y:y2}];
	};
}
